#include "mySort.h"
#include <iostream>
#include <string>

MySort::MySort() {
    num = 0;
    origin = NULL;
}

MySort::MySort(int num, int* origin) {
    this->num = num;
    this->origin = new int[num];
    for (int i = 0; i < num; i++)
        this->origin[i] = origin[i];
}

MySort::~MySort() {
    num = 0;
    if (origin)
        delete[] origin;
    origin = NULL;
}

void MySort::changeSwap(int& a, int& b) {
    int c = a;
    a = b;
    b = c;
}

string MySort::printNum() {
    string res;
    for (int i = 0; i < num; i++)
    {
        res += to_string(origin[i]);
        if (i != num - 1)res.push_back(' ');
    }
    res.push_back('\n');
    return res;
}

string MySort::paritySort() {
    string result;
    bool isSorted = false;
    while (!isSorted) {
        isSorted = true;
        for (int i = 1; i <= num - 2; i += 2) {
            if (origin[i] > origin[i + 1]) {
                changeSwap(origin[i], origin[i + 1]);
                isSorted = false;
                result += this->printNum();
            }
        }
        for (int i = 0; i <= num - 2; i += 2) {
            if (origin[i] > origin[i + 1]) {
                changeSwap(origin[i], origin[i + 1]);
                isSorted = false;
                result += this->printNum();
            }
        }

    }
    return result;
}

string MySort::bubbleSort() {
    string result;
    int left = 0, right = num - 1;
    bool isSorted = false;
    while (!isSorted && left < right) {
        isSorted = true;
        for (int i = left; i < right; i++) {
            if (origin[i] > origin[i + 1]) {
                changeSwap(origin[i], origin[i + 1]);
                isSorted = false;
                result += this->printNum();
            }
        }
        right--;
        for (int i = right; i > left; i--) {
            if (origin[i] < origin[i - 1]) {
                changeSwap(origin[i], origin[i - 1]);
                isSorted = false;
                result += this->printNum();
            }
        }
        left++;

    }
    return result;
}
